package com.sxkf.template.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.third.im.entity.Message;
import com.sxkf.core.third.im.entity.SendMessage;
import com.sxkf.core.third.im.service.YxService;
import com.sxkf.template.constants.ViewTypeConstants;
import com.sxkf.template.entity.*;
import com.sxkf.template.mapper.OrderMapper;
import com.sxkf.template.service.*;
import org.jeecg.common.exception.SxkfException;
import org.jeecg.common.exception.SxkfExceptionEnum;
import org.jeecg.common.util.CheckEmptyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private ProductService productService;

/*    @Resource
    private OrderService orderService;*/

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderDetailService orderDetailService;

    @Resource
    private ShoppingCartService shoppingCartService;

    @Resource
    private ProductLikeService productLikeService;

    @Resource
    private ProductViewService productViewService;

    @Resource
    private YxService yxService;



    @Override
    public IPage<Product> productPage(Integer pageNo, Integer pageSize, String userId, String searchType, String storeId) {
        // 搜索类型： 1=店铺商品 2=浏览记录 3=已购买 4=购物车 5=我的收藏
        Page<Product> page = new Page<>(pageNo, pageSize);
        List<String> productIdList;
        List<Product> productList;
                switch (searchType){
            case "1":
                // 店铺商品
                page = productService.page(new Page<>(pageNo,pageSize),new LambdaQueryWrapper<Product>()
                        .eq(Product::getStoreId,storeId));
                break;
            case "2":
                // 浏览记录
                Page<ProductView> productViewPage = productViewService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<ProductView>()
                        .eq(ProductView::getUserId, userId)
                        .eq(ProductView::getViewType, ViewTypeConstants.PRODUCT));
                BeanUtils.copyProperties(productViewPage,page,"records");
                if (productViewPage.getTotal() == 0L){
                    break;
                }
                productIdList = productViewPage.getRecords()
                        .stream()
                        .map(ProductView::getViewId)
                        .collect(Collectors.toList());
                if (CheckEmptyUtil.isEmpty(productIdList)){
                    page.setRecords(new ArrayList<>());
                }else {
                    productList = productService.list(new LambdaQueryWrapper<Product>()
                            .in(Product::getId, productIdList)
                    );

                    page.setRecords(productList);
                }

                break;
            case "3":
                Page<ProductBuyed> pageBuy = new Page<>(pageNo, pageSize);
                // 已购买
                Page<OrderDetail> orderDetailPage = orderDetailService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<OrderDetail>()
                        .eq(OrderDetail::getUserId, userId)
                        .eq(OrderDetail::getStoreId, storeId)
                );
                BeanUtils.copyProperties(orderDetailPage,pageBuy,"records");
                if (orderDetailPage.getTotal() == 0L){
                    break;
                }
                Set<String> productIdSet = orderDetailPage.getRecords()
                        .stream()
                        .map(OrderDetail::getProductId)
                        .collect(Collectors.toSet());
                if (CheckEmptyUtil.isEmpty(productIdSet)){
                    pageBuy.setRecords(new ArrayList<>());
                }else {

                    List<String> orderIdList = orderDetailPage.getRecords()
                            .stream()
                            .map(OrderDetail::getOrderId).collect(Collectors.toList());
                    List<Order> orderList = orderMapper.selectBatchIds(orderIdList);
                    Map<String, List<OrderDetail>> map = orderDetailPage.getRecords()
                            .stream()
                            .collect(Collectors.groupingBy(OrderDetail::getProductId));
//                    Map<String, String> map = orderDetailPage.getRecords()
//                            .stream()
//                            .collect(Collectors.toMap(OrderDetail::getProductId, OrderDetail::getOrderId));
                    Map<String, Order> orderMap = orderList.stream().collect(Collectors.toMap(Order::getId, order -> order));

                    productList = productService.list(new LambdaQueryWrapper<Product>()
                            .in(Product::getId, productIdSet)
                    );
                    List<ProductBuyed> buyedList = new ArrayList<>();
                    ProductBuyed productBuyed = new ProductBuyed();
                    productList.forEach(item -> {
                        BeanUtils.copyProperties(item,productBuyed);
                        Order order = orderMap.get(map.get(item.getId()).get(0).getOrderId());
                        productBuyed.setBuyTime(order.getPayTime());
                        productBuyed.setOrderNo(order.getOrderNo());
                        buyedList.add(productBuyed);
                    });
//                BeanUtils.copyProperties(orderDetailPage,page,"records");
                    pageBuy.setRecords(buyedList);
                }

                break;
            case "4":
                // 购物车
                Page<ShoppingCart> shoppingCartPage = shoppingCartService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<ShoppingCart>()
                        .eq(ShoppingCart::getUserId, userId)
                );
                BeanUtils.copyProperties(shoppingCartPage,page,"records");
                if (shoppingCartPage.getTotal() == 0L){
                    break;
                }
                productIdList = shoppingCartPage.getRecords()
                        .stream()
                        .map(ShoppingCart::getProductId)
                        .collect(Collectors.toList());
                if (CheckEmptyUtil.isEmpty(productIdList)){
                    page.setRecords(new ArrayList<>());
                }else {
                    productList = productService.list(new LambdaQueryWrapper<Product>()
                            .in(Product::getId, productIdList)
                    );
//                BeanUtils.copyProperties(shoppingCartPage,page,"records");
                    page.setRecords(productList);
                }

                break;
            case "5":
                // 我的收藏
                Page<ProductLike> productLikePage = productLikeService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<ProductLike>()
                        .eq(ProductLike::getUserId, userId)
                );
                BeanUtils.copyProperties(productLikePage,page,"records");
                if (productLikePage.getTotal() == 0L){
                    break;
                }
                productIdList = productLikePage
                        .getRecords()
                        .stream()
                        .map(ProductLike::getProductId)
                        .collect(Collectors.toList());

                if (CheckEmptyUtil.isEmpty(productIdList)){
                    page.setRecords(new ArrayList<>());
                }else {
                    productList = productService.list(new LambdaQueryWrapper<Product>()
                            .in(Product::getId, productIdList)
                    );
//                BeanUtils.copyProperties(productLikePage,page,"records");
                    page.setRecords(productList);
                }

                break;
            default:
                throw new SxkfException(SxkfExceptionEnum.PARAM_ERROR);
        }



        return page;
    }

    @Override
    public Boolean notice(String senderId, String receiverId, String orderNo) {

        SendMessage sendMessage = new SendMessage();

        Message message = new Message();
        message.setMessage_type(0);
        message.setText("订单号："+orderNo+"请发下货，谢谢！");

        sendMessage.setMessage(message);

        Boolean b = yxService.sendMessage(senderId, receiverId, sendMessage);
        return b;
    }


}
